package capricornus.components.sound
{
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.utils.Dictionary;

	public class SoundManager
	{
		public static const instance:SoundManager = new SoundManager();

		private var _soundState:Boolean = true;
		private var _soundVolume:Number = 0.8;
		
		private var _dict:Dictionary;
		
		public function SoundManager()
		{
			init();
		}
		
		private function init():void
		{
			_dict = new Dictionary();
		}
		
		public function set soundState( value:Boolean ):void
		{
			if( _soundState != value )
			{
				_soundState = value;
				SoundMixer.soundTransform = new SoundTransform( value?_soundVolume:0 );
			}
		}
		
		public function set soundVolume( value:Number ):void
		{
			if( _soundVolume != value )
			{
				if( value < 0 ) value = 0;
				if( value > 1 ) value = 1;
				
				_soundVolume = value;
				
				if( _soundState )
				{
					SoundMixer.soundTransform = new SoundTransform( _soundVolume );
				}
			}
		}
		
		/**
		 * 
		 * @param url 声音路径
		 * @param time 播放次数,默认是1次，值为-1时表示不停地播放
		 * @param delayTimeOnComplete 播放下一次的延迟间隔，ms
		 * 
		 */		
		public function playSound( url:String, time:int=1, delayTimeOnComplete:uint=4000, isCheckLoad:Boolean=true ):void
		{
			var s:MySound = _dict[url];
			if( s==null )
			{
				s = new MySound( url, delayTimeOnComplete, isCheckLoad );
				_dict[url] = s;
			}
			s.play( time );
		}
	}
}














